package com.shujia.student;

import java.io.*;

public class SplitFIle {
    public static void main(String[] args) throws Exception {

        long start = System.currentTimeMillis();


        /**
         * 切分文件将一个文件切分成多个
         * 切分的过程可以理解为将数据存到hdfs的过程
         *
         * 4G ---> 8个文件
         *
         * 1、计算出每一个文件多少行数据
         *
         *
         *
         */

        int fileNum = 8;

        File file = new File("java/data/big_student.txt");

        Long length = fileLength(file);


        //每个文件行数   每读取l行数据生成一个新的文件
        Long l = length / fileNum;

        int i = 1;

        long flag = 1;
        FileWriter fileWriter = new FileWriter("java/data/tmp/" + i);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);


        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {

            bufferedWriter.write(line);
            bufferedWriter.newLine();


            flag++;
            if (flag == l && i <= 7) {

                //关闭之前的文件对象
                bufferedWriter.flush();
                bufferedWriter.close();
                fileWriter.close();


                //切换文件对象
                i++;
                fileWriter = new FileWriter("java/data/tmp/" + i);
                bufferedWriter = new BufferedWriter(fileWriter);

                flag = 1;
            }

        }

        bufferedWriter.flush();
        bufferedWriter.close();
        fileWriter.close();


        long end = System.currentTimeMillis();

        System.out.println(end - start);


    }

    /**
     * 统计文件的行数
     *
     * @param file 文件
     * @return
     */
    public static Long fileLength(File file) throws Exception {
        Long count = 0L;


        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            count++;
        }


        return count;
    }
}
